package com.suda.controller;

import com.github.pagehelper.PageInfo;
import com.suda.pojo.Packages;
import com.suda.service.PackagesService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/packages")
@CrossOrigin
public class PackagesController {

    @Autowired
    private PackagesService packagesService;

    /***
     * Packages分页条件搜索实现
     * @param packages
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Packages packages, @PathVariable  int page, @PathVariable  int size){
        //调用PackagesService实现分页条件查询Packages
        PageInfo<Packages> pageInfo = packagesService.findPage(packages, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Packages分页搜索实现
     * @param page:当前页
     * @param limit:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search" )
    public Result<PageInfo> findPage(int page, int limit){
        //调用PackagesService实现分页查询Packages
        PageInfo<Packages> pageInfo = packagesService.findPage(page, limit);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param packages
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Packages>> findList(@RequestBody(required = false) Packages packages){
        //调用PackagesService实现条件查询Packages
        List<Packages> list = packagesService.findList(packages);
        return new Result<List<Packages>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用PackagesService实现根据主键删除
        packagesService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Packages数据
     * @param packages
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Packages packages, @PathVariable String id){
        //设置主键值
        packages.setPackageId(id);
        //调用PackagesService实现修改Packages
        packagesService.update(packages);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Packages数据
     * @param packages
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Packages packages){
        //调用PackagesService实现添加Packages
        packagesService.add(packages);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Packages数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Packages> findById(@PathVariable String id){
        //调用PackagesService实现根据主键查询Packages
        Packages packages = packagesService.findById(id);
        return new Result<Packages>(true,StatusCode.OK,"查询成功",packages);
    }

    /***
     * 查询Packages全部数据
     * @return
     */
    @GetMapping
    public Result<List<Packages>> findAll(){
        //调用PackagesService实现查询所有Packages
        List<Packages> list = packagesService.findAll();
        return new Result<List<Packages>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
